import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
// 服务
import { AlertService } from './alert.service';

import { Observer } from 'rxjs/Observer';
@Component({
    moduleId: module.id,
    selector: 'myalert',
    templateUrl: 'alert.component.html'
})

export class MyAlertComponent implements OnInit {
    @ViewChild('alertModal') public alertModal: ModalDirective;
    title: string;
    msg: string;
    observer: Observer<any>;
    typ: string;
    result: boolean;
    constructor(private _alertSvc: AlertService) {

    }
    ngOnInit(): void {
        // this.getPosition();
       // console.log(`MyAlertComponent ngOnInit`);
        this._alertSvc.getAlert().subscribe(({ title: title, msg: msg, observer: o, typ: typ }) => {
            this.title = title;
            this.msg = msg;
            this.alertModal.show();
            this.observer = o;
            this.typ = typ;
        });
        this._alertSvc.getHide().subscribe(({ hide: boolean }) => {
            // console.log(`status+${status}`);
            this.alertModal.hide();
        });
    }
    onHidden(): void {
        this.observer.next(this.result);
        this.observer.complete();
    }
    hide(r: boolean): void {
        this.result = r;
        this._alertSvc.hide();
    }
}